﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TfsCommitMonitor.Tests.Mocking;
using TfsCommitMonitor.Tfs;

namespace TfsCommitMonitor.Tests
{
	[TestClass]
	public class ProjectStoreTest
	{
		[TestMethod]
		public void DeletePersistedDataTest()
		{
			List<Project> projects = new List<Project>();

			var project = new Project(new MockTfsBusiness())
			{
				CollectionUri = new Uri("http://testcollection"),
				ProjectPath = "$/Project1",
				RefreshTimeInMinutes = 15,
			};

			projects.Add(project);
			projects.PersistToDisk();

			ProjectStore.DeletePersistedData();

			const bool expected = false;
			bool actual = ProjectStore.HasPersistedData();

			Assert.AreEqual(expected, actual);
		}

		[TestMethod]
		public void HasPersistedDataTest()
		{
			List<Project> projects = new List<Project>();

			var project = new Project(new MockTfsBusiness())
			{
				CollectionUri = new Uri("http://testcollection"),
				ProjectPath = "$/Project1",
				RefreshTimeInMinutes = 15,
			};

			projects.Add(project);
			projects.PersistToDisk();

			bool expected = true;
			bool actual = ProjectStore.HasPersistedData();

			Assert.AreEqual(expected, actual);
			
			ProjectStore.DeletePersistedData();

			expected = false;
			actual = ProjectStore.HasPersistedData();

			Assert.AreEqual(expected, actual);
		}

		[TestMethod]
		public void PersistToAndLoadFromDiskTest()
		{
			ProjectStore.DeletePersistedData();
			
			List<Project> expected = new List<Project>();

			var project1 = new Project(new MockTfsBusiness())
			{
				CollectionUri = new Uri("http://testcollection"),
				ProjectPath = "$/Project1/",
				RefreshTimeInMinutes = 15,
			};

			project1.PollForChanges();

			var project2 = new Project(new MockTfsBusiness())
			{
				CollectionUri = new Uri("http://testcollection"),
				ProjectPath = "$/Project2/",
				RefreshTimeInMinutes = 15,
			};

			project2.PollForChanges();

			expected.Add(project1);
			expected.Add(project2);			
			expected.First().Changesets.Last().Read = true;

			expected.PersistToDisk();

			List<Project> actual = ProjectStore.LoadFromDisk();

			Assert.IsTrue(expected.SequenceEqual(actual));
		}
	}
}
